package Persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import Controlador.Utils;
import Negocio.CerealCosechaGruesa;
import Negocio.Mineral;


public class AdmPersistenciaCerealCosechaGruesa extends AdministradorPersistencia 
{
	private static AdmPersistenciaCerealCosechaGruesa instancia;
	
	private AdmPersistenciaCerealCosechaGruesa()
	{
		
	}
	public static AdmPersistenciaCerealCosechaGruesa getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaCerealCosechaGruesa();
		return instancia;
	}


	public void borrar(Object d) 
	{
		try
		{
			//CerealCosechaGruesa a = (CerealCosechaGruesa)d;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaGruesa set estado = ? where codigo = ?");
			s.setString(1, Utils.CONSTANTE_INACTIVO);
			s.setLong(2, (Long)d);
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		

	}

	@Override
	public Long insertar(Object o) 
	{
		try
		{
			CerealCosechaGruesa a = (CerealCosechaGruesa)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
PreparedStatement s = con.prepareStatement("insert into cereal values (?,?,?,?,?,?,?)");
			
			//agregar campos
			
			s.setString(1, a.getNombre());
			s.setInt(2, a.getInicioEpocaSiembra());
			s.setLong(3,a.getMineral().getCodigo());
			s.setInt(4, a.getFinEpocaSiembra());
			s.setDouble(5,a.getRinde());
			s.setDouble(6, a.getDesperdicio());
			s.setString(7,a.getEstado());;
			
			s.execute();
			
			s = con.prepareStatement("select max(codigo) from cereal");
			ResultSet result = s.executeQuery();
			Long res = null;
			if (result.next())
				res = result.getLong(1);
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return res;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		return null;

		

	}

	@Override
	public Vector<Object> buscarTodos() 
	{		
		try
		{
			CerealCosechaGruesa a = null;
			Vector<Object> vec = new Vector<Object>();
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  cereal where desperdicio <> ? ");
			s.setInt(1, Utils.CONSTANTE_COSECHAFINA);
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));	
				
				a = new CerealCosechaGruesa (
						result.getLong(1),
						result.getString(2),
						result.getInt(3),
						result.getInt(5),
						result.getDouble(6),
						result.getString(8),
						result.getDouble(7),
						m
							);
				vec.add(a);
			
			}
			
			
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return vec;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}

	}
	
	@Override
	public void modificar(Object o) 
	{
		try
		{
			CerealCosechaGruesa a = (CerealCosechaGruesa)o;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update  cosechaGruesa set nombre = ?, inicioEpoca = ?, finEpoca = ?, rinde = ?, estado = ?, desperdicio = ? where codigo = ?" );
			
			//agregar campos --
			s.setString(1, a.getNombre());
			
			s.setInt(2, a.getInicioEpocaSiembra());
			s.setInt(3, a.getFinEpocaSiembra());
			s.setDouble(4, a.getRinde());
			s.setString(5, a.getEstado());
			s.setDouble(6, a.getDesperdicio());
			s.setLong(7, a.getCodigo());
			
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println();
		}
		


	}
	
	@Override
	public Object buscarPorCodigo(Object o) {
		try
		{
			CerealCosechaGruesa a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from cereal where desperdicio <> 0 and codigo = ?");
			s.setLong(1,(Long) o);
			ResultSet result = s.executeQuery();
			if (result.next())
			{
				Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));	
				
				a = new CerealCosechaGruesa (
						result.getLong(1),
						result.getString(2),
						result.getInt(3),
						result.getInt(5),
						result.getDouble(6),
						result.getString(8),
						result.getDouble(7),
						m
							);
				
			}else{
				//TODO Exception
				System.out.println("No se encuentra el Cereal cosecha Gruesa");
				return null;
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}

	}
	public Object buscarPorNombre(Object o) {
		try
		{
			CerealCosechaGruesa a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from  cosechaGruesa where nombre = ?");
			s.setString(1,(String) o);
			ResultSet result = s.executeQuery();
			if (result.next())
			{
				Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));	
				
				a = new CerealCosechaGruesa (
						result.getLong(1),
						result.getString(2),
						result.getInt(3),
						result.getInt(5),
						result.getDouble(6),
						result.getString(8),
						result.getDouble(7),
						m
							);
				
			}else{
				//TODO Exception
				System.out.println("No se encuentra el Cereal cosecha Gruesa");
				return null;
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}

	}
	

	
	public Vector<CerealCosechaGruesa> selectAll() {
		try
		{
			Vector<CerealCosechaGruesa> a = new Vector<CerealCosechaGruesa>();
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select * from cereal");
			ResultSet result = s.executeQuery();
		    while (result.next())
			{
				if(result.getDouble(7)!= 0){
	
				Mineral m = (Mineral) AdmPersistenciaMineral.getInstancia().buscarPorCodigo(result.getLong(4));	
				a.add(new CerealCosechaGruesa (
						result.getLong(1),
						result.getString(2),
						result.getInt(3),
						result.getInt(5),
						result.getDouble(6),
						result.getString(8),
						result.getDouble(7),
						m
							));

				}
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			//TODO Exception
			System.out.println("Error extraccion");
			return null;
		}
	}

}
